﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace 同步器
{
    public partial class FormTongBu : Form
    {
        MouseHook hook = new MouseHook();
        KeyboardHookLib _keyboardHook;
        Keys nowKey = new Keys();
        int ClickCount = 0;
        int lastX = -1;
        int lastY = -1;
        public FormTongBu()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            hook = new MouseHook();
            hook.OnMouseActivity += Hook_OnMouseActivity;
            hook.Start();
        }

        private void Hook_OnMouseActivity(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                hook.Stop();
                Thread.Sleep(500);
                IntPtr hwnd = Win32.GetForegroundWindow();
                textBox1.Text += "," + hwnd.ToString();
            }
        }

        /// <summary>
        /// 客户端键盘捕捉事件.
        /// </summary>
        /// <param name="hookStruct">由Hook程序发送的按键信息</param>
        /// <param name="handle">是否拦截</param>
        public void OnKeyPress(KeyboardHookLib.HookStruct hookStruct, int wParam, out bool handle)
        {
            handle = false; //预设不拦截任何键

            Keys key = (Keys)hookStruct.vkCode;
            if (nowKey == key && wParam == 256)
            {
                return;
            }
            Console.WriteLine("wParam=" + wParam.ToString());
            int keyCode = (int)Convert.ToChar(key);

            string[] list = textBox1.Text.Split(',');
            for (int i = 0; i < list.Length; i++)
            {
                if (list[i] != "")
                {
                    string text = list[i];
                    Thread thread = new Thread(() =>
                    {
                        if (wParam == 256)
                        {
                            ClickKey(new IntPtr(Convert.ToInt32(text)), keyCode);
                        }
                        else
                        {
                            UpKey(new IntPtr(Convert.ToInt32(text)), keyCode);
                            nowKey = new Keys();
                        }
                    });
                    thread.Start();
                }
            }
            nowKey = key;
        }

        private void ClickKey(IntPtr intPtr, int key)
        {
            Win32.SendMessage(intPtr, Win32.WM_KEYDOWN, key, 0);
        }


        private void UpKey(IntPtr intPtr, int key)
        {
            Win32.SendMessage(intPtr, Win32.WM_KEYUP, key, 0);
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "开启")
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("未设置窗口");
                    return;
                }
                //安装勾子
                _keyboardHook = new KeyboardHookLib();
                _keyboardHook.InstallHook(this.OnKeyPress);

                hook = new MouseHook();
                hook.OnMouseActivity += Hook_OnMouseTongBu;
                hook.Start();


                button2.Text = "暂停";
                textBox1.Enabled = false;
                ClickCount = 0;
                timer1.Start();
            }
            else
            {
                _keyboardHook.UninstallHook();
                hook.Stop();
                button2.Text = "开启";
                textBox1.Enabled = true;
                ClickCount = 0;
                timer1.Stop();
            }
        }

        private void Hook_OnMouseTongBu(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                ClickCount++;
                lastX = e.X;
                lastY = e.Y;
            }
        }

        private void ClickMouse(string hwnd, int x, int y, int clickCount)
        {

            Rect rect;
            Win32.GetWindowRect(new IntPtr(Convert.ToInt32(hwnd.Trim().Replace(",", ""))), out rect);
            if (clickCount == 1)
            {
                Win32.SendMessage(new IntPtr(Convert.ToInt32(hwnd.Trim().Replace(",", ""))), 0x0201, 1, Convert.ToInt32(x - rect.Left + ((y - rect.Top - 20) << 16)));
                //Thread.Sleep(5);
                Win32.SendMessage(new IntPtr(Convert.ToInt32(hwnd.Trim().Replace(",", ""))), 0x0202, 1, Convert.ToInt32(x - rect.Left + ((y - rect.Top - 20) << 16)));
            }
            else if (clickCount==2)
            {
                Win32.SendMessage(new IntPtr(Convert.ToInt32(hwnd.Trim().Replace(",", ""))), 0x0203, 1, Convert.ToInt32(x - rect.Left + ((y - rect.Top - 20) << 16)));
            }

        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            string[] list = textBox1.Text.Split(',');
            for (int i = 0; i < list.Length; i++)
            {
                if (list[i] != "")
                {
                    int X = lastX;
                    int Y = lastY;
                    string text = list[i];
                    switch (ClickCount)
                    {
                        case 0:
                            break;
                        case 1:
                            Thread thread = new Thread(() =>
                            {
                                ClickMouse(text, X, Y, 1);
                            });
                            thread.Start();
                            break;
                        default:
                            Thread threadDouble = new Thread(() =>
                            {
                                ClickMouse(text, X, Y, 2);
                            });
                            threadDouble.Start();
                            break;
                    }
                }
            }
            ClickCount = 0;
            lastX = -1;
            lastY = -1;
        }
    }
}
